// pages/movies/more-movie/more-movie.ts
var app = getApp();
const {
  http
} = require('../../../utils/util');
var util = require('../../../utils/util');
Page({
  data: {
    movies: {},
    navigateTitle: "",
    requestUrl: "",
    totalCount: 1,
    isEmpty: true,
    triggered: false
  },
  onLoad: function (options) {
    var category = options.category;
    // 从onLoad传递给onReady，使用中间值data传递
    this.data.navigateTitle = category;
    var dataUrl = "";
    switch (category) {
      case "正在热映":
        dataUrl = app.globaData.doubanBase + "/top?type=Imdb&skip=0&limit=10&lang=Cn";
        break;
      case "即将上映":
        dataUrl = app.globaData.doubanBase + "/top?type=Imdb&skip=0&limit=10&lang=Cn";
        break;
      case "豆瓣TOP250":
        dataUrl = app.globaData.doubanBase + "/top?type=Imdb&skip=0&limit=10&lang=Cn";
        break;
    }
    this.data.requestUrl = dataUrl;
    util.http(dataUrl, this.processDoubanData);
    
  },
  // 上滑加载更多
  onScrollLower: function (event) {
    var nextUrl = this.data.requestUrl + "?pageIndex=" + this.data.totalCount + "&pageSize=20";
    util.http(nextUrl, this.processDoubanData);
    // 导航栏加载图标
    wx.showNavigationBarLoading();
  },
  // 下拉刷新数据
  onPullDownRefresh: function (event) {
    this.data.isEmpty = true;
    this.data.movies = {};
    this.data.totalCount=0;
    var refreshUrl = this.data.requestUrl + "?pageIndex=1&pageSize=20";
    util.http(refreshUrl, this.processDoubanData);
    wx.showNavigationBarLoading();

  },
   // 跳转电影更多页面
   onMoreTap: function (event) {
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
      url: '../more-movie/more-movie?category=' + category,
    })
  },
   // 跳转电影详情页
   onMovieTap: function (event) {
    var movieId = event.currentTarget.dataset.movieid;
    console.log(movieId)
    wx.navigateTo({
      url: '../movie-detail/movie-detail?id=' + movieId,
    })
  },
  processDoubanData: function (moviesDouban) {
    var movies = [];
    for (var idx in moviesDouban) {
      var subject = moviesDouban[idx];
      for (var i in subject.data) {
        var data = subject.data[i];
        var name = data.name;
        if (name.length > 6) {
          name = name.substring(0, 6) + "...";
        }
        var temp = {
          title: name,
          movieId: subject.doubanId,
          coverageUrl: data.poster,
          average: subject.imdbRating,
          stars: util.convertToStarsArray(subject.rottenVotes)
        }
        movies.push(temp);
      }
    var totalMovies = {};
    // 把新数据和旧的合并
    if (!this.data.isEmpty) {
      totalMovies = this.data.movies.concat(movies);
    } else {
      totalMovies = movies;
      this.data.isEmpty = false;
    }
    this.setData({
      movies: totalMovies
    })
    this.data.totalCount += 1;
    // 关闭导航栏加载图标
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  }
},
  // onLoad:页面还在加载中，不添加ui效果
  // onReady:页面渲染完成
  onReady: function (event) {
    // 动态设置导航栏标题
    wx.setNavigationBarTitle({
      title: this.data.navigateTitle,
    })
  }
})